#include <tsin/runtime/BulletBlitWork.hpp>

#include <tsin/runtime/GameRuntime.hpp>
#include <tsin/runtime/BulletRuntime.hpp>

#include <tsin/display/Display.hpp>
#include <tsin/display/Image.hpp>
#include <tsin/display/DecoratedImage.hpp>
#include <tsin/library/Game.hpp>

#include <tsin/util/Set.hpp>
#include <tsin/util/MemCheck.hpp>
using namespace tsin;

BulletBlitWork::BulletBlitWork()
{
}

BulletBlitWork::~BulletBlitWork()
{
        for (set->toBegin(); !set->atEnd(); set->next()) {
                delete set->remove();
        }
}

void BulletBlitWork::init(GameRuntime *runtime)
{
        this->display = &runtime->display;
        this->viewport = runtime->game->getViewport(0);

        set = runtime->enemy_bullets;
}

int BulletBlitWork::run()
{
        display->useViewportFrame(*viewport);
        for (set->toBegin(); !set->atEnd(); set->next()) {
                BulletRuntime *bullet = set->get();
                bullet->checkEvents();
                if (bullet->stage == BULLET_STAGE_DIED) {
                        delete set->remove();
                        continue;
                }
                if (!bullet->image)
                        continue;
                display->blitImage(*bullet->image, bullet);
        }
        return 0;
}
